package com.fzx.fzwsupplysystem.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 描述：    显示Toast工具类，正常连续显示，
 * 后期添加自定义Toast显示
 * 项目名称：AndroidUtilsDemo
 * 创建人：  FangZhixin
 * 创建时间：2017/12/28
 */

public class ToastUtil {

    private static Toast toast;

    /**
     * 连续立即显示Toast
     *
     * @param context 上下文
     * @param msg     显示内容
     */
    @SuppressLint("ShowToast")
    public static void showToast(Context context, String msg) {
        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    /**
     * 连续立即显示Toast
     *
     * @param context 上下文
     * @param msg     显示内容
     */
    @SuppressLint("ShowToast")
    public static void showLongToast(Context context, String msg) {
        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        }
        toast.setText(msg);
        toast.show();
    }

    /**
     * 正常显示Toast
     *
     * @param context 上下文
     * @param msg     显示内容
     */
    public static void showShort(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 正常显示Toast
     *
     * @param context 上下文
     * @param msg     显示内容
     */
    public static void showLong(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    /**
     * 定时显示Toast
     *
     * @param context 上下文
     * @param msg     显示信息
     * @param time    long，显示时长，
     */
    @SuppressLint("ShowToast")
    public static void showMyToast(Context context, String msg, long time) {
        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        }
        toast.setText(msg);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                toast.cancel();
            }
        }, time);
    }

    public static void showCustomToast(Context context, String msg, long time) {
        if (toast == null) {
            toast = new Toast(context);
        }
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setText(msg);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                toast.cancel();
            }
        }, time);
    }

    public static void showCustomToast(Context context, String msg, int gravity, long time) {
        if (toast == null) {
            toast = new Toast(context);
        }
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(gravity, 0, 0);
        toast.setText(msg);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                toast.cancel();
            }
        }, time);
    }


    public static void showToastView(Context context, View view) {
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

}
